<?php

//error_reporting(0);

class Web_Admin_Productos_Svc_ActualizarProducto
{

    public function doIt()
    {
        $this->actualizar();
    }

    private function actualizar()
    {
        if ($_POST['pro_nombre'] == '') {
            $error['pro_nombre'] = 'Ingrese Nombre';
        }
        if ($_POST['pro_descripcion'] == '') {
            $error['pro_descripcion'] = 'Ingrese Descripcion';
        }
        if ($_POST['pro_precio_s'] == '') {
            $error['pro_precio_s'] = 'Ingrese Precio Soles';
        }
        if (count($error) > 0) {
            $_SESSION['post'] = $_POST;
            $_SESSION['error'] = $error;
            Ey::redirect($_SERVER['HTTP_REFERER']);
        }

        $pro_id = Ey::getPrm(4);

        $obj = new Web_Db_Productos();

        $pro_key = str_replace('’', '', $_POST['pro_nombre']);
        $pro_keye = str_replace("'", "", $pro_key);
        $pro_keyi = str_replace("/", "", $pro_keye);

        $stranges = array('&aacute;', '&eacute;', '&iacute;', '&oacute;', '&uacute;', '&Aacute;', '&Eacute;', '&Iacute;', '&Oacute;', '&Uacute;', '&ntilde;', '&Ntilde;');

        $fixed = array('á', 'é', 'í', 'ó', 'ú', 'Á', 'É', 'Í', 'Ó', 'Ú', 'ñ', 'Ñ');

        $_POST['pro_descripcion'] = str_replace($stranges, $fixed, $_POST['pro_descripcion']);
        
        $_POST['pro_restriccion'] = str_replace($stranges, $fixed, $_POST['pro_restriccion']);

        $row = array('pro_nombre' => $_POST['pro_nombre'],
                    'pro_precio' => $_POST['pro_precio_s'],
                    'pro_stock' => $_POST['pro_stock'],
                    'pro_meta' => $_POST['pro_meta'],
                    'pro_descripcion' => $_POST['pro_descripcion'],
                    'pro_restriccion' => $_POST['pro_restriccion'],
                    'pro_key' => Ey::urlAmigable($pro_keyi));

        $obj->update($row, 'pro_id=' . $pro_id);

        $handle = new Ey_Upload($_FILES['imagen']);
        if ($handle->uploaded) {
            $handle->file_new_name_body = 'pro_' . $pro_id;
            $handle->file_new_name_ext = 'jpg';
            $handle->image_resize = true;
            $handle->image_ratio_crop = true;
            $handle->image_x = 450;
            $handle->image_y = 450;
            $handle->process(APP_ROOT . DS . '_data/img/productos');
//            $handle->clean();
        }
        
        $handleSmall = new Ey_Upload($_FILES['imagen']);
        if ($handleSmall->uploaded) {
            $handleSmall->file_new_name_body = 'pro_' . $pro_id . '_small';
            $handleSmall->file_new_name_ext = 'jpg';
            $handleSmall->image_resize = true;
            $handleSmall->image_ratio_crop = true;
            $handleSmall->image_x = 150;
            $handleSmall->image_y = 150;
            $handleSmall->process(APP_ROOT . DS . '_data/img/productos');
            $handleSmall->clean();
        }
        
        $handleMedio = new Ey_Upload($_FILES['imgMedio']);
        if ($handleMedio->uploaded) {
            $handleMedio->file_new_name_body = 'pro_' . $pro_id . '_m';
            $handleMedio->file_new_name_ext = 'jpg';
            $handleMedio->image_resize = true;
            $handleMedio->image_ratio_crop = true;
            $handleMedio->image_x = 300;
            $handleMedio->image_y = 300;
            $handleMedio->process(APP_ROOT . DS . '_data/img/productos');
            $handleMedio->clean();
        }
        
        $handle2 = new Ey_Upload($_FILES['img2']);
        if ($handle2->uploaded) {
            $handle2->file_new_name_body = 'pro_' . $pro_id . '_2';
            $handle2->file_new_name_ext = 'jpg';
            $handle2->image_resize = true;
            $handle2->image_ratio_crop = true;
            $handle2->image_x = 450;
            $handle2->image_y = 450;
            $handle2->process(APP_ROOT . DS . '_data/img/productos');
            $handle2->clean();
        }
        
        $handle3 = new Ey_Upload($_FILES['img3']);
        if ($handle3->uploaded) {
            $handle3->file_new_name_body = 'pro_' . $pro_id . '_3';
            $handle3->file_new_name_ext = 'jpg';
            $handle3->image_resize = true;
            $handle3->image_ratio_crop = true;
            $handle3->image_x = 450;
            $handle3->image_y = 450;
            $handle3->process(APP_ROOT . DS . '_data/img/productos');
            $handle3->clean();
        }
        
        $handle4 = new Ey_Upload($_FILES['img4']);
        if ($handle4->uploaded) {
            $handle4->file_new_name_body = 'pro_' . $pro_id . '_4';
            $handle4->file_new_name_ext = 'jpg';
            $handle4->image_resize = true;
            $handle4->image_ratio_crop = true;
            $handle4->image_x = 450;
            $handle4->image_y = 450;
            $handle4->process(APP_ROOT . DS . '_data/img/productos');
            $handle4->clean();
        }

        Ey::redirect(WEB_ROOT . "/admin/productos");
    }

}